package com.product.shop.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.product.shop.dao.UserDao;
import com.product.shop.util.JwtUtil;
import com.product.shop.util.MD5Util;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.product.shop.entity.UserEntity;
import com.product.shop.service.UserService;
import com.product.common.utils.PageUtils;
import com.product.common.utils.R;



/**
 * 
 *
 * @author brh
 * @email sunlightcs@gmail.com
 * @date 2022-02-22 16:30:11
 */
@RestController
@RequestMapping("shop/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserDao userDao;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 登录
     */
    @RequestMapping("/info")
    public R info(@RequestBody UserEntity user){
        //首先判断是否存在该用户
        UserEntity user1 =userDao.findByUser(user.getNickname());
        if (user1!=null){
            if (user1.getPassword().equals(user.getPassword())){
                String token = jwtUtil.createJWT(user1.getId() + "", user1.getNickname(), "user");
                user1.setLoginCount(user1.getLoginCount()+1);
                user1.setLastLoginData(new Date());
                userDao.updateById(user1);
                redisTemplate.opsForValue().set(user.getNickname(),token);
                return R.ok().put("user",user1).put("token",token);
            }else{
                return R.error("用户名或密码错误!");
            }
        }else{
            return R.error("用户不存在!");
        }
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
        //加盐
        String s = MD5Util.md5Code(user.getPassword(), 3);
        user.setSalt(s);
        user.setRegisterData(new Date());
        userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }



}
